//
// Created by 32827 on 2024/1/11.
//
#include "yzl_ui.h"
#ifdef YZL_UI_ILI9341
#include "yzl_ili9341.h"
#include "yzl_font.h"

YZF_RESULT yzl_ui_init(YZ_COLOR background){
    yzl_ili9341_init();
    YZL_UI_Background=background;
    yzl_ui_clear_screen();
    YZL_ILI9341_IS_INIT=YZF_TRUE;
    return YZF_OK;
}
YZF_RESULT yzl_ui_clear_screen(void){
    yzl_ili9341_clear(0,0,YZL_SCREEN_WIDTH,YZL_SCREEN_HEIGHT,YZL_UI_Background);
    return YZF_OK;
}
YZF_RESULT yzl_ui_fill_screen(YZ_COLOR color){
    yzl_ili9341_clear(0,0,YZL_SCREEN_WIDTH,YZL_SCREEN_HEIGHT,color);
    return YZF_OK;
}
YZF_RESULT yzl_ui_draw_rect(uint16_t x,uint16_t y,uint16_t width,uint16_t height,YZF_BOOL isFill,YZ_COLOR color){
    yzl_ili9341_draw_rect(x,y,width,height,isFill,color);
    return YZF_OK;
}
YZF_RESULT yzl_ui_draw_circle(uint16_t x,uint16_t y,uint16_t radius,YZF_BOOL isFill,struct Yz_Color color){
    yzl_ili9341_draw_circle(x,y,radius,isFill,color);
    return YZF_OK;
}
YZF_RESULT yzl_ui_draw_line(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,struct Yz_Color color){
    yzl_ili9341_draw_line(x1,y1,x2,y2,color);
    return YZF_OK;

}
YZF_RESULT yzl_ui_draw_pixel(uint16_t x1,uint16_t y1,struct Yz_Color color){
    yzl_ili9341_draw_pixel(x1,y1,color);
    return  YZF_OK;
}
YZF_RESULT yzl_ui_draw_pixels(uint16_t x1,uint16_t y1,uint16_t width,uint16_t height,YZ_COLOR *img){
    yzl_ili9341_open_window(x1,y1,width,height);
    yzl_ili9341_nwrite_cmd(CMD_SetPixel);
    for (uint16_t j = 0; j < width*height; j++) {
        yzl_ili9341_nwrite_data((img+j)->data);
    }
    return YZF_OK;
}
YZF_RESULT yzl_ui_draw_string(uint16_t x,uint16_t y,uint16_t limitY,uint16_t *str,uint16_t len,YZ_COLOR back_color,YZ_COLOR text_color){
    if(Yz_FontInitFlag!=YZF_TRUE){
        return YZF_ERROR;
    }
    uint16_t startX=x,startY=y;
    int tempLen=len;
    uint16_t wlen = 0;
    while (len!=0) {

        if(len>YZL_FONT_SRAM_SIZE){
            wlen=YZL_FONT_SRAM_SIZE;
            len-=YZL_FONT_SRAM_SIZE;
        }else{
            wlen=len;
            len=0;
        }
        if (yzl_font_get_pixels(&str[tempLen-len-wlen], yzl_font_sram, wlen) == YZF_OK) {
            for (int m = 0; m < wlen; m++) {
                if(str[tempLen-len-wlen+m]=='\000'){
                    continue;
                }else
                if(str[tempLen-len-wlen+m]=='\r'){
                    startX=0;
                    continue;
                }else
                if(str[tempLen-len-wlen+m]=='\n'){
                    startY+=YZL_FONT_LINE_HEIGHT;
                    startX=x;
                    continue;
                }
                if (startX + yzl_font_sram[m].xOffset < 0 || (startY + yzl_font_sram[m].yOffset) < 0) {
                    yzl_ili9341_open_window(startX, startY, yzl_font_sram[m].width, yzl_font_sram[m].height);
                } else {
                    yzl_ili9341_open_window(startX + yzl_font_sram[m].xOffset, startY + yzl_font_sram[m].yOffset, yzl_font_sram[m].width,
                                            yzl_font_sram[m].height);
                }
                yzl_ili9341_nwrite_cmd(CMD_SetPixel);
                int count = 0;
                int pixelIndex = 0;
                for (int i = 0; i < yzl_font_sram[m].height; i++) {
                    for (int j = 0; j < yzl_font_sram[m].width; j++) {
                        if (count >= 8) {
                            pixelIndex++;
                            count = 0;
                        }
                        if ((yzl_font_sram[m].pixel[pixelIndex] & (0b1 << (7 - count))) != 0) {
                            yzl_ili9341_nwrite_data(text_color.data);
                        } else {
                            yzl_ili9341_nwrite_data(back_color.data);
                        }
                        count++;
                    }
                }

                startX = startX + yzl_font_sram[m].xAdvance;

                if (startX + YZL_FONT_SIZE > limitY) {
                    startY+=YZL_FONT_LINE_HEIGHT;
                    startX=x;
                }
            }
        } else {
            return YZF_ERROR;
        }
    }
    return YZF_OK;
}
#endif